package com.bysj.sausys.service.impl;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.beans.Pagination;
import com.bysj.sausys.dao.SysDicParamsMapper;
import com.bysj.sausys.exception.ParamException;
import com.bysj.sausys.model.SysDicParams;
import com.bysj.sausys.service.ISysDicParamsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 字典Service接口实现
 *
 * @author WangZK
 */
@Service("sysDicParamsService")
public class SysDicParamsService implements ISysDicParamsService {

    @Resource
    private SysDicParamsMapper sysDicParamsMapper;

    /**
     * 保存字典类型
     *
     * @param sysDicParams
     * @param username
     */
    @Override
    public void saveDicType(SysDicParams sysDicParams, String username) {
        if (checkDicTypeExist(sysDicParams.getId(), sysDicParams.getPmappname(), sysDicParams.getPmname())) {
            throw new ParamException("字典类型已存在，保存失败");
        }

        sysDicParams.setPmname(sysDicParams.getPmappname());
        sysDicParams.setInsertMan(username);
        sysDicParams.setInsertTime(new Date());
        sysDicParamsMapper.insertSelective(sysDicParams);
    }

    /**
     * 保存字典数据
     *
     * @param sysDicParams
     * @param username
     */
    @Override
    public void saveDicParams(SysDicParams sysDicParams, String username) {
        //校验数据
        if (checkDicTypeExist(sysDicParams.getId(), sysDicParams.getPmappname(), sysDicParams.getPmname())) {
            throw new ParamException("字典数据已存在，保存失败");
        }

        sysDicParams.setInsertMan(username);
        sysDicParams.setInsertTime(new Date());
        sysDicParamsMapper.insertSelective(sysDicParams);
    }

    /**
     * 判断字典是否已存在
     *
     * @param id
     * @param pmappname
     * @param pmname
     * @return
     */
    private boolean checkDicTypeExist(Integer id, String pmappname, String pmname) {
        return sysDicParamsMapper.countByPmappnameAndPmname(pmappname, pmname, id) > 0;
    }

    /**
     * 更新字典类型
     *
     * @param sysDicParams
     * @param username
     */
    @Override
    public void updateDicType(SysDicParams sysDicParams, String username) {
        //进行数据校验
        SysDicParams before = sysDicParamsMapper.selectByPrimaryKey(sysDicParams.getId());
        if (before == null) {
            throw new ParamException("待更新的字典分类不存在");
        }
        if (checkDicTypeExist(sysDicParams.getId(), sysDicParams.getPmappname(), sysDicParams.getPmname())) {
            throw new ParamException("字典类型已存在，更新失败");
        }

        SysDicParams after = new SysDicParams();

        after.setId(sysDicParams.getId());
        after.setPmappname(sysDicParams.getPmappname());
        after.setPmname(sysDicParams.getPmappname());
        after.setPmvalue(sysDicParams.getPmvalue());
        after.setStatus(before.getStatus());
        after.setRemarks(sysDicParams.getRemarks());
        after.setLastupdMan(username);
        after.setLastupdTime(new Date());

        sysDicParamsMapper.updateByPrimaryKeySelective(after);
    }

    /**
     * 更新字典数据
     *
     * @param sysDicParams
     * @param username
     */
    @Override
    public void updateDicParams(SysDicParams sysDicParams, String username) {
        //进行数据校验
        SysDicParams before = sysDicParamsMapper.selectByPrimaryKey(sysDicParams.getId());
        if (before == null) {
            throw new ParamException("待更新的字典数据不存在");
        }
        if (checkDicTypeExist(sysDicParams.getId(), sysDicParams.getPmappname(), sysDicParams.getPmname())) {
            throw new ParamException("字典数据已存在，更新失败");
        }

        SysDicParams after = new SysDicParams();

        after.setId(sysDicParams.getId());
        after.setPmappname(sysDicParams.getPmappname());
        after.setPmname(sysDicParams.getPmname());
        after.setPmvalue(sysDicParams.getPmvalue());
        after.setStatus(before.getStatus());
        after.setRemarks(sysDicParams.getRemarks());
        after.setLastupdMan(username);
        after.setLastupdTime(new Date());

        sysDicParamsMapper.updateByPrimaryKeySelective(after);
    }


    /**
     * 分页获取字典类型
     *
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult getDicTypeForPage(PageQuery pageQuery) {
        int count = sysDicParamsMapper.countDicType();
        if (count > 0) {
            List<SysDicParams> sysDicParamsList = sysDicParamsMapper.getDicTypeForPage(pageQuery);
            PageResult<SysDicParams> result = new PageResult<>();
            result.setList(sysDicParamsList);
            Pagination pagination = new Pagination();
            pagination.setTotal(count);
            pagination.setPageSize(pageQuery.getPageSize());
            pagination.setCurrentPage(pageQuery.getPageNo());
            result.setPagination(pagination);
            return result;
        }
        return new PageResult<>();
    }

    /**
     * 根据id获取字典表数据
     *
     * @param id
     * @return
     */
    @Override
    public SysDicParams findById(Integer id) {
        SysDicParams sysDicParams = sysDicParamsMapper.selectByPrimaryKey(id);
        if (sysDicParams == null) {
            throw new ParamException("获取字典数据失败");
        }
        return sysDicParams;
    }

    /**
     * 根据id删除字典类型
     *
     * @param id
     */
    @Override
    public void deleteDicType(Integer id) {
        SysDicParams sysDicParams = sysDicParamsMapper.selectByPrimaryKey(id);
        //判断是否存在子分类，如果存在不能进行删除
        if (checkDisChild(sysDicParams.getPmappname())) {
            throw new ParamException("不能删除，该分类下存在子分类");
        }
        sysDicParamsMapper.deleteByPrimaryKey(id);
    }

    /**
     * 判断字典类型是否存在子分类
     *
     * @param pmappname
     * @return
     */
    private boolean checkDisChild(String pmappname) {
        return sysDicParamsMapper.countDisChild(pmappname) > 0;
    }

    /**
     * 设置字典表数据状态
     *
     * @param id
     * @param status
     * @param currentUsername
     */
    @Override
    public void setDicParamsStatus(Integer id, int status, String currentUsername) {
        Map params = new HashMap(4);
        params.put("status", status);
        params.put("lastupdTime", new Date());
        params.put("lastupdMan", currentUsername);
        sysDicParamsMapper.updateStatusByPrimaryKey(id, params);
    }

    /**
     * 分页获取字典数据
     *
     * @param pmappname
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult getDicParamsForPage(String pmappname, PageQuery pageQuery) {
        int count = sysDicParamsMapper.countDicParams(pmappname);
        if (count > 0) {
            List<SysDicParams> sysDicParamsList = sysDicParamsMapper.getDicParamsForPage(pmappname, pageQuery);
            PageResult<SysDicParams> result = new PageResult<>();
            result.setList(sysDicParamsList);
            Pagination pagination = new Pagination();
            pagination.setTotal(count);
            pagination.setPageSize(pageQuery.getPageSize());
            pagination.setCurrentPage(pageQuery.getPageNo());
            result.setPagination(pagination);
            return result;
        }
        return new PageResult<>();
    }

    /**
     * 根据项值获取字典数据
     *
     * @param categoryId
     * @return
     */
    @Override
    public SysDicParams findByPmname(String categoryId) {
        return sysDicParamsMapper.findByPmname(categoryId);
    }

    /**
     * 根据字典类型获取字典数据
     *
     * @param pmappname
     * @return
     */
    @Override
    public List<SysDicParams> getDicParamsByPmappname(String pmappname) {
        int count = sysDicParamsMapper.countPmappnameAndPmnameNoSameByPmappname(pmappname);
        if (count > 0) {
            return sysDicParamsMapper.getPmappnameAndPmnameNoSameByPmappname(pmappname);
        }
        return new ArrayList<>();
    }

    /**
     * 获取所有启用的字典分类
     */
    @Override
    public List<SysDicParams> getAllDicType() {
        return sysDicParamsMapper.getAllDicType();
    }

    /**
     * 根据字典类型获取双层级联的字典数据
     *
     * @param pmappname
     * @return
     */
    @Override
    public List<Map> getDoubleLayerCascadeDicParams(String pmappname) {
        return sysDicParamsMapper.getDoubleLayerCascadeDicParams(pmappname);
    }
}
